import ToolRequest from '@/base/newToolAxios';
import RequestOption from '@/base/RequestOption';
import { getActToolUrl } from '@/base/constant';
const tr = new ToolRequest();

export default class UserDao {
  /**
   * 查询用户。
   *
   * @param  userId   用户ID
   * @param userName 用户名
   * @param phone    手机号
   * @param projectType    项目类型
   * @param userType 用户类型
   *
   * @return 查找到的用户列表。
   */
  async findUser(
    userId: number,
    userName: string,
    phone: string,
    projectType: number,
    userType: number
  ) {
    console.log(`[UserDAO] 查询用户, userName: ${userName}`);
    const ro = new RequestOption();
    return await tr.get(
      getActToolUrl(),
      {
        action: 'findUser',
        userId: userId,
        userName: userName,
        phone: phone,
        projectType: projectType,
        userType: userType
      },
      ro
    );
  }
  
  /**
   * 查询班级内的学生
   * @param classId 班级ID
   * @param teamId 小组ID
   * @param studentId 学生ID
   * @param userName 学生名称
   * @param haveTeam 是否有小组
   * @param currentPage 当前页
   * @param pageSize 每页条数
   * @param studentNo 学号
   * @param college 学院
   * @param major 专业
   * @param className 班级
   * @returns 学生列表
   */
  async findClassStudents(
    classId: number,
    teamId: number,
    studentId: number,
    userName: string,
    haveTeam: boolean,
    currentPage: number,
    pageSize: number,
    studentNo?: string,
    college?: string,
    major?: string,
    className?: string
  ) {
    console.log(`[UserDAO] 查询班级(${classId})内的学生, 小组: ${teamId || '全部'}, 页码: ${currentPage}`);
    console.log(`[UserDAO] 查询参数: studentId=${studentId}, userName=${userName}, haveTeam=${haveTeam}`);
    console.log(`[UserDAO] 附加参数: studentNo=${studentNo}, college=${college}, major=${major}, className=${className}`);
    
    const ro = new RequestOption();
    try {
      const response = await tr.get(
        getActToolUrl(),
        {
          action: 'findClassStudents',
          classId: classId,
          teamId: teamId,
          studentId: studentId,
          userName: userName,
          haveTeam: haveTeam,
          currentPage: currentPage,
          pageSize: pageSize,
          studentNo: studentNo,
          college: college,
          major: major,
          className: className
        },
        ro
      );
      
      // 记录服务器响应，帮助调试
      console.log(`[UserDAO] 查询班级学生响应: code=${response?.data?.code}, 数据条数=${response?.data?.data?.rows?.length || 0}, 总数=${response?.data?.data?.totalCount || 0}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[UserDAO] 查询班级学生接口返回错误: ${response?.data?.msg || '未知错误'}`);
      }
      
      return response;
    } catch (error) {
      console.error('[UserDAO] 查询班级学生异常:', error);
      throw error;
    }
  }

  /**
   * 设置学生所在小组
   * @param classId 班级ID
   * @param teamId 小组ID
   * @param studentIds 学生ID数组
   * @returns 操作结果
   */
  async setTeamForStudents(classId: number, teamId: number, studentIds: Array<number>) {
    console.log(`[UserDAO] 设置${studentIds.length}名学生到小组(${teamId}), 班级: ${classId}`);
    const ro = new RequestOption();
    return await tr.post(
      getActToolUrl(),
      {
        action: 'setTeamForStudents',
        classId: classId,
        teamId: teamId,
        studentIds: studentIds.join(',')
      },
      ro
    );
  }

  /**
   * 查询系统中所有学生（可选择未在指定班级中的学生）
   * @param classId 班级ID（用于排除已在该班级的学生）
   * @param studentId 学生ID
   * @param studentName 学生名称
   * @param current 当前页
   * @param pageSize 每页条数
   * @param studentNo 学号
   * @param college 学院
   * @param major 专业
   * @param className 班级
   * @returns 学生列表
   */
  async findAllStudents(
    classId: number,
    studentId: number,
    studentName: string,
    current: number,
    pageSize: number,
    studentNo?: string,
    college?: string,
    major?: string,
    className?: string
  ) {
    console.log(`[UserDAO] 查询所有学生, 排除班级: ${classId}, 名称: ${studentName || '全部'}, 页码: ${current}`);
    console.log(`[UserDAO] 查询参数: studentId=${studentId}, studentNo=${studentNo}, college=${college}, major=${major}, className=${className}`);
    
    const ro = new RequestOption();
    try {
      // 记录请求参数
      const requestParams = {
        action: 'findAllStudents',
        classId: classId,
        studentId: studentId,
        studentName: studentName,
        currentPage: current,
        pageSize: pageSize,
        studentNo: studentNo,
        college: college,
        major: major,
        className: className
      };
      console.log('[UserDAO] 发送请求参数:', JSON.stringify(requestParams));
      
      const response = await tr.get(
        getActToolUrl(),
        requestParams,
        ro
      );
      
      // 记录服务器响应，帮助调试
      console.log(`[UserDAO] 查询学生响应: code=${response?.data?.code}, 数据条数=${response?.data?.data?.rows?.length || 0}, 总数=${response?.data?.data?.totalCount || 0}`);
      
      if (response?.data?.code !== 200) {
        console.warn(`[UserDAO] 查询学生接口返回错误: ${response?.data?.msg || '未知错误'}`);
      } else if (!response?.data?.data?.rows || response?.data?.data?.rows.length === 0) {
        console.warn('[UserDAO] 查询学生接口返回空数据');
      }
      
      return response;
    } catch (error) {
      console.error('[UserDAO] 查询学生异常:', error);
      throw error;
    }
  }
}
